import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-09-03
 * Time: 21:03
 */
public class demo2 {
    //生产者消费者模型
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1000);
        Thread creator = new Thread(() -> {
            int i = 1;
            while (true) {
                try {
                    queue.put(i);
                    System.out.println("生产第" + i + "个元素");
                    i++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread user = new Thread(() -> {

            while (true) {
                try {
                    int i = queue.take();
                    System.out.println("消费第" + i + "个元素");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        creator.start();
        user.start();
    }
}
